package cn.lblbc.lib.utils

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.util.TypedValue
import android.view.View
import android.view.inputmethod.EditorInfo
import android.widget.EditText
import android.widget.ImageView
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import com.bumptech.glide.Glide
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.text.SimpleDateFormat
import java.util.*

fun <T> launch(action: suspend () -> T, onSuccess: ((T) -> Unit)? = null) {
    GlobalScope.launch(Dispatchers.IO) {
        try {
            val resp = action.invoke()
            withContext(Dispatchers.Main) {
                onSuccess?.invoke(resp)
            }
        } catch (e: Exception) {
            if (e.message?.isNotEmpty() == true) {
                ToastUtil.toast(e.message!!)
            }
        }
    }
}

fun Context.openBrowser(url: String) {
    if (url.isNotEmpty()) {
        val intent = Intent()
        intent.action = "android.intent.action.VIEW"
        intent.data = Uri.parse(url)
        startActivity(intent)
    }
}

fun Fragment.loadImage(imageView: ImageView, url: String) {
    Glide.with(this).load(url).into(imageView)
}

fun loadImage(context: Context, imageView: ImageView, url: String) {
    Glide.with(context).load(url).into(imageView)
}

fun showFragment(fragmentManager: FragmentManager, container: Int, fragment: Fragment) {
    val ft = fragmentManager.beginTransaction()
    ft.add(container, fragment)
    ft.commit()
}

fun EditText.onSearchKeyDown(action: () -> Unit) {
    setOnEditorActionListener { _, keyCode, _ ->
        if (keyCode == EditorInfo.IME_ACTION_SEARCH) {
            action()
        }
        true
    }
}

fun Context.showDeleteDialog(content: String, action: () -> Unit) {
    val alertDialog = AlertDialog.Builder(this).setMessage(content).setCancelable(false)
        .setPositiveButton("删除") { _, _ -> action() }
        .setNegativeButton("取消") { _, _ -> }
        .create()
    alertDialog.show()
}

fun Fragment.showDeleteDialog(content: String, action: () -> Unit) {
    val alertDialog = AlertDialog.Builder(requireContext()).setMessage(content).setCancelable(false)
        .setPositiveButton("删除") { _, _ -> action() }
        .setNegativeButton("取消") { _, _ -> }
        .create()
    alertDialog.show()
}

fun View.show(toShow: Boolean) {
    visibility = if (toShow) View.VISIBLE else View.GONE
}

fun View.show() {
    visibility = View.VISIBLE
}

fun View.hide() {
    visibility = View.GONE
}

fun formatTime(time: Long, format: String): String {
    val sdf = SimpleDateFormat(format)
    return sdf.format(Date(time))
}

fun dp2Px(context: Context, dp: Float): Int {
    return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.resources.displayMetrics).toInt()
}